package com.qiwx.pigeon.fragment.query;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.jaygoo.widget.RangeSeekBar;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.LogUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.ScreenUtils;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.fragment.AbsQueryFragment;

import org.angmarch.views.NiceSpinner;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import fj.dropdownmenu.lib.ion.ViewInject;
import fj.dropdownmenu.lib.ion.ViewUtils;
import fj.dropdownmenu.lib.utils.DropdownUtils;
import fj.dropdownmenu.lib.view.DropdownButton;
import fj.dropdownmenu.lib.view.DropdownColumnView;

import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_END;
import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_START;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-28 10:52
 * 鸽笼情况查询
 **/
public class QueryCageFragment_bak extends AbsQueryFragment {
    @BindView(R.id.spinner_wh)
    NiceSpinner spinnerWh;
    @BindView(R.id.txt_start_date)
    TextView txtStartDate;
    @BindView(R.id.txt_end_date)
    TextView txtEndDate;
    @BindView(R.id.btn_condition)
    Button btnCondition;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.rel_query_tools_top)
    RelativeLayout rel_query_tools_top;
    PopupWindow popupWindow;
    //无精窝数
    int nofertilization_range_start;
    int nofertilization_range_end;
    //死精窝数
    int deadfertilization_range_start;
    int deadfertilization_range_end;
    //孵怀窝数
    int badfertilization_range_start;
    int badfertilization_range_end;
    //产蛋量
    int layegg_range_start;
    int layegg_range_end;
    @ViewInject(R.id.drop_filter_1)
    @BindView(R.id.drop_filter_1)
    DropdownButton dropFilter1;
    @ViewInject(R.id.drop_filter_2)
    @BindView(R.id.drop_filter_2)
    DropdownButton dropFilter2;
    @ViewInject(R.id.drop_filter_3)
    @BindView(R.id.drop_filter_3)
    DropdownButton dropFilter3;
    @ViewInject(R.id.drop_filter_4)
    @BindView(R.id.drop_filter_4)
    DropdownButton dropFilter4;
    @BindView(R.id.mask)
    View mask;
    @ViewInject(R.id.drop_nofertilization)
    @BindView(R.id.drop_nofertilization)
    DropdownColumnView dropdownColumnView1;
    @ViewInject(R.id.drop_deadfertilization)
    @BindView(R.id.drop_deadfertilization)
    DropdownColumnView dropdownColumnView2;
    @ViewInject(R.id.drop_badfertilization)
    @BindView(R.id.drop_badfertilization)
    DropdownColumnView dropdownColumnView3;
    @ViewInject(R.id.drop_layegg)
    @BindView(R.id.drop_layegg)
    DropdownColumnView dropdownColumnView4;

    @ViewInject(R.id.drop_filter_5)
    @BindView(R.id.drop_filter_5)
    DropdownButton dropFilter5;
    @ViewInject(R.id.drop_fertilizationed)
    @BindView(R.id.drop_fertilizationed)
    DropdownColumnView dropdownColumnView5;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_query_cage_bak;
    }

    @Override
    public void initAfterOnCreate() {
        initView();
        initFilter();
    }


    private void initFilter() {
        DropdownUtils.initFragment(getActivity(), this, view, mask);
        ViewUtils.injectFragmentViews(this, view, mask);
        //分类
        dropdownColumnView1.setSingleRow(selectObj -> {
            LogUtil.e("TAG", selectObj.getValue());
        })
                .setSingleRowList(QueryFilterData.getWJData(), -1)  //单列数据
                .setButton(dropFilter1) //按钮
                .show();
        dropdownColumnView2.setSingleRow(selectObj -> {
            LogUtil.e("TAG", selectObj.getValue());
        })
                .setSingleRowList(QueryFilterData.getSJData(), -1)  //单列数据
                .setButton(dropFilter2) //按钮
                .show();
        dropdownColumnView3.setSingleRow(selectObj -> {
            LogUtil.e("TAG", selectObj.getValue());

        })
                .setSingleRowList(QueryFilterData.getFHData(), -1)  //单列数据
                .setButton(dropFilter3) //按钮
                .show();
        dropdownColumnView4.setSingleRow(selectObj -> {
            LogUtil.e("TAG", selectObj.getValue());

        })
                .setSingleRowList(QueryFilterData.getCDData(), -1)  //单列数据
                .setButton(dropFilter4) //按钮
                .show();
        dropdownColumnView5.setSingleRow(selectObj -> {
            LogUtil.e("TAG", selectObj.getValue());
        })
                .setSingleRowList(QueryFilterData.getData5(), -1)  //单列数据
                .setButton(dropFilter5) //按钮
                .show();

    }

    private void initView() {
        btnCondition.setVisibility(View.GONE);
        spinnerWh.setPadding(ScreenUtils.dip2px(getActivity(), 7), 0, 0, 0);
        initSpinner();

    }

    private void initSpinner() {
        List<WareHouseBean> list = getWareHouses();
        List<String> data = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            data.add(list.get(i).getName());
        }
        spinnerWh.attachDataSource(data);
        spinnerWh.setOnSpinnerItemSelectedListener((parent, view, position, id) -> {
            MyToast.showCenterShort(list.get(position).getId() + "");

        });
    }

    public int[] getSeekValue(DecimalFormat decimalFormat, RangeSeekBar seekBar) {
        int[] result = new int[2];
        result[0] = Integer.valueOf(decimalFormat.format(seekBar.getLeftSeekBar().getProgress()));
        result[1] = Integer.valueOf(decimalFormat.format(seekBar.getRightSeekBar().getProgress()));
        return result;
    }

    private void showPopWindow() {
        if (popupWindow == null) {
            View contentView = getLayoutInflater().inflate(R.layout.popwindow_cage_condition, null);
            RangeSeekBar nofertilization = contentView.findViewById(R.id.seekbar_nofertilization);
            RangeSeekBar deadfertilization = contentView.findViewById(R.id.seekbar_deadfertilization);
            RangeSeekBar badfertilization = contentView.findViewById(R.id.seekbar_badfertilization);
            RangeSeekBar layegg = contentView.findViewById(R.id.seekbar_lay_egg);
            //无精窝数
            nofertilization.setIndicatorTextDecimalFormat("0");
            //死精窝数
            deadfertilization.setIndicatorTextDecimalFormat("0");
            //孵坏窝数
            badfertilization.setIndicatorTextDecimalFormat("0");
            //产蛋量
            layegg.setIndicatorTextDecimalFormat("0");
            DecimalFormat df = new DecimalFormat("0");
            popupWindow = new PopupWindow(contentView,
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, false);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);
            popupWindow.setAnimationStyle(R.style.popwin_anim_style);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //无精
                    int[] nofertilizations = getSeekValue(df, nofertilization);
                    nofertilization_range_start = nofertilizations[0];
                    nofertilization_range_end = nofertilizations[1];
                    //死精
                    int[] deadfertilizations = getSeekValue(df, deadfertilization);
                    deadfertilization_range_start = deadfertilizations[0];
                    deadfertilization_range_end = deadfertilizations[1];
                    //孵坏
                    int[] adfertilizations = getSeekValue(df, badfertilization);
                    badfertilization_range_start = adfertilizations[0];
                    badfertilization_range_end = adfertilizations[1];
                    //产蛋量
                    int[] layeggs = getSeekValue(df, layegg);
                    layegg_range_start = layeggs[0];
                    layegg_range_end = layeggs[1];
                }
            });
        }
        popupWindow.showAsDropDown(rel_query_tools_top);
//        int offsetY = ScreenUtils.dip2px(getActivity(), 150);
//        PopupWindowCompat.showAsDropDown(popupWindow, rel_query_tools_top, 0, offsetY, Gravity.END);

    }

    //根据条件查询鸽笼情况
    public void queryCages() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_START:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra("car_Time_MSG"))) {
                    txtStartDate.setText(data.getStringExtra("car_Time_MSG"));
                }
                break;
            case REQUEST_CODE_END:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra("car_Time_MSG"))) {
                    txtEndDate.setText(data.getStringExtra("car_Time_MSG"));
                }
                break;
        }
    }


    @OnClick({R.id.txt_start_date, R.id.txt_end_date, R.id.btn_condition, R.id.btn_query})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_start_date:
                selectStartValiDate();
                break;
            case R.id.txt_end_date:
                selectEndValiDate(txtStartDate.getText().toString());
                break;
            case R.id.btn_condition:
                showPopWindow();
                break;
            case R.id.btn_query:
                queryCages();
                break;
        }
    }

}
